package com.songshuai.webapi.common.aop;


import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.common.redis.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BaseSSOAspect {

    List<String> permist=Arrays.asList("/api/user/queryFunctionByRole","/api/user/queryNavigationInfo","/api/user/queryFunctionInfo");

    public Object getQrResponse(ProceedingJoinPoint proceedingJoinPoint,HttpServletRequest request,Authentication authentication,RedisUtil redisUtil,String SuffixKey) throws Throwable{
        String authorization = request.getHeader("Authorization");
        Map<String,Object> result = new ConcurrentHashMap<>();
        System.out.println("====uri is"+request.getRequestURI());
        if(permist.contains(request.getRequestURI())){
            Object proceed = proceedingJoinPoint.proceed();
            return proceed;
        }
        if (StringUtils.isNotBlank(authorization)) {

            Object principal = authentication.getPrincipal();
            String name = String.valueOf(principal);
            String id = redisUtil.getString(name);
            if(StringUtils.isBlank(id)){
                return new QueryResponse(404,"请重新登录");
            }else{
                String token = redisUtil.getString(id);
                String[] strings = authorization.split(" ");
                if(strings.length>0){
                    String auth=strings[strings.length-1];
                    if(StringUtils.isNotBlank(token)){

//                        System.out.println("auth:" + auth);
//                        System.out.println("=====token:"+token);
                        if (token.equalsIgnoreCase(auth.trim())) {
                            if("crm".equalsIgnoreCase(SuffixKey)){
                                redisUtil.expire(name, 1800);
                                redisUtil.expire(id, 1800);
                                redisUtil.setSuffixKey("permission");
                                redisUtil.expire(name, 1800);
                                redisUtil.expire(id, 1800);
                                redisUtil.setSuffixKey(SuffixKey);
                            }else{
                                redisUtil.expire(name, 1800);
                                redisUtil.expire(id, 1800);
                            }
                            Object proceed = proceedingJoinPoint.proceed();
                            return proceed;
                        } else {//token过期或者在别的地方登陆了
                            return new QueryResponse(404,"请重新登录");
                        }
                    }else{
                        return new QueryResponse(400,"登录不成功重新登录");
                    }
                }else{
                    return new QueryResponse(401,"访问此资源需要完全的身份验证");
                }
            }
        }else{//请求没带token
            return new QueryResponse(401,"请求此资源需要完全的身份验证");
        }
    }


}
